package _case

import (
	"encoding/base64"
	"encoding/hex"
	"encoding/json"
	"fmt"
)

type User struct {
	ID   int64
	Name string
	Age  uint8
}

func EncodingCase() {
	//json序列化
	u1 := User{1, "Tom", 18}
	bytes, err := json.Marshal(u1)
	fmt.Println(bytes, err)
	//json反序列化
	u2 := User{}
	err = json.Unmarshal(bytes, &u2)
	fmt.Println(u2, err)

	//base64编解码
	base64Str := base64.StdEncoding.EncodeToString(bytes)
	fmt.Println(base64Str)
	bytes1, err := base64.StdEncoding.DecodeString(base64Str)
	fmt.Println(bytes1, err)

	//16进制编解码
	hexStr := hex.EncodeToString(bytes)
	fmt.Println(hexStr)
	bytes2, err := hex.DecodeString(hexStr)
	fmt.Println(bytes2, err)
}
